package com.boxin.ims.modules.wechat.busi;

import java.util.List;

import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.ImageMessage;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.UserBusiness;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;


@Service
public class UrlProcessor  extends BusinessProcessor{

	@Override
	public ProcessMessage process() {
		
		WechatMember  member = null ;
		//是否需要检查会员 
		if(isNeedBeMember()){
			memberCheckAndReply();
		}
		
		
		
		String reply = "";
		String upcon = textMessage.getContent();
		UserBusiness userBusiness = WechatUtils.getUserBusinessByKeyWords(user.getId(),upcon);
		//图文回复
			if(userBusiness!=null && ImageMessage.MSG_TYPE_NEWS.equals(userBusiness.getMsgType())){
				List<NewsReply>  newsReplyList =  findNewsMessage(member,userBusiness);
				if(newsReplyList!=null  && !newsReplyList.isEmpty()){
					for(NewsReply newsReply : newsReplyList){
						
						if(member!=null){
							String title = newsReply.getTitle();
							if(StringUtils.isNotEmpty(title)){
								title = title.replaceAll("%连续签到%", member.getSignupCount()+"");
								if(0==member.getSignupCount()){
									member.setSignupCount(1);
								}
								title = title.replaceAll("%连续签到%", member.getSignupCount()+"");
								newsReply.setTitle(title);
							}
							
							String desc = newsReply.getDescription();
							desc = desc.replaceAll("%连续签到%", member.getSignupCount()+"");
							newsReply.setDescription(desc);
							
						}
					}
					
					//更新URL 将系统中动态的参数值补充到URL中
					replaceUrlParameters(newsReplyList);
					
					writeMultiImagesToUser(newsReplyList);
					
					return null;
					
			} 
		} 
		
			
			//回复
			if(StringUtils.isNotEmpty(reply)){
				
				reply = replaceUrlParameters(reply);
				writeToUser(reply);
			}else{
				writeToUser("该服务还未启动.");
			}	
				
				
				
		
		
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		
		return this.isNeedMember;
	}
	
	

}
